//---------------------------------------------------------------------
// <copyright file="ODataVerboseJsonCollectionSerializer.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------

namespace Microsoft.Data.OData.VerboseJson
{
    #region Namespaces
    #endregion Namespaces

    /// <summary>
    /// OData Verbose JSON serializer for collections.
    /// </summary>
    internal sealed class ODataVerboseJsonCollectionSerializer : ODataVerboseJsonPropertyAndValueSerializer
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="verboseJsonOutputContext">The output context to write to.</param>
        internal ODataVerboseJsonCollectionSerializer(ODataVerboseJsonOutputContext verboseJsonOutputContext)
            : base(verboseJsonOutputContext)
        {
            DebugUtils.CheckNoExternalCallers();
        }

        /// <summary>
        /// Writes the start of a collection.
        /// </summary>
        internal void WriteCollectionStart()
        {
            DebugUtils.CheckNoExternalCallers();

            // at the top level, we need to write the "results" wrapper for V2 and higher and for responses only
            if (this.WritingResponse && this.Version >= ODataVersion.V2)
            {
                // { "results":
                this.JsonWriter.StartObjectScope();
                this.JsonWriter.WriteDataArrayName();
            }

            // Write the start of the array for the collection items
            // "["
            this.JsonWriter.StartArrayScope();
        }

        /// <summary>
        /// Writes the end of a collection.
        /// </summary>
        internal void WriteCollectionEnd()
        {
            DebugUtils.CheckNoExternalCallers();

            // Write the end of the array for the collection items
            // "]"
            this.JsonWriter.EndArrayScope();

            // at the top level, we need to close the "results" wrapper for V2 and higher and for responses only
            if (this.WritingResponse && this.Version >= ODataVersion.V2)
            {
                // "}"
                this.JsonWriter.EndObjectScope();
            }
        }
    }
}